Psst: Open the JavaScript Console and try to play around with these functions:
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
matrix[0][0] // returns 1
matrix[0][1] // returns 2
matrix[1][0] // returns 4
const grid = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
];
for (let row of grid) {
for (let item of row) {
console.log(item);
}
}
// should log the ff. into the console:
// 1, 2, 3, 4, 5,
// 6, 7, 8, 9, 10,
// 11, 12, 13, 14, 15,
// 16, 17, 18, 19, 20,
// 21, 22, 23, 24, 25
depthFirstSearch2D(grid); // start from 1
// should return:
// [1, 2, 3, 4, 5,
// 10, 15, 20, 25, 24,
// 19, 14, 9, 8, 13,
// 18, 23, 22, 17, 12,
// 7, 6, 11, 16, 21]
depthFirstSearch2D(grid, 2, 2); // start from 13
// should return:
// [13, 8, 3, 4, 5,
// 10, 15, 20, 25, 24,
// 19, 14, 9, 18, 23,
// 22, 17, 12, 7, 2,
// 1, 6, 11, 16, 21]
breadthFirstSearch2D(grid); // start from 1
// should return:
// [1, 2, 6, 3, 7,
// 11, 4, 8, 12, 16,
// 5, 9, 13, 17, 21,
// 10, 14, 18, 22, 15,
// 19, 23, 20, 24, 25]
breadthFirstSearch2D(grid, 2, 2); // start from 13
// should return:
// [13, 8, 14, 18, 12,
// 3, 9, 7, 15, 19,
// 23, 17, 11, 4, 2,
// 10, 6, 20, 24, 22,
// 16, 5, 1, 25, 21]